package com.sx.micowell.module.sport.engine

import com.example.mvvm.until.LogUtils
import com.sx.micowell.framework.entity.ActionContainer
import com.sx.micowell.framework.entity.SportChangeData
import com.sx.micowell.framework.model.Sport2Model
import com.sx.micowell.framework.tools.cmd.CmdUntil

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/12/4 15:05
 */
class SportEngineImpl(viewModel: Sport2Model) : SportEngine {
    var viewModel: Sport2Model

    var actionContainer: MutableList<ActionContainer> = mutableListOf()

    var kcal: String = "0"

    var finish = false

    var currentCount: Int = 0

    init {
        this.viewModel = viewModel
        viewModel.sportList.forEach {
            var value = ActionContainer(it.id, it.actionCount.toInt(), it.setCount)
            actionContainer.add(value)
        }
        viewModel.currentPosition.value = 0
        upDataUI()
        LogUtils.i("开始第一个动作！Go!")
    }

    fun addKCal(cal: Int) {
        kcal = kcal.toBigDecimal().add(CmdUntil.countCalories(cal)).toPlainString()
    }

    fun upDataUI() {
        var data = actionContainer.get(viewModel.currentPosition.value!!)
        var uiData = SportChangeData(
            viewModel.currentPosition.value!!,
            actionContainer.size,
            data.actionCount,
            data.setCount,
            kcal,
            data.finishCount,
            data.finishGroup
        )
        viewModel.sportDataUi.value = uiData
    }

    /**如果当前的数量大于该动作的次数的时候，需要判断当前的组数是否完成，
     * 如果组数已经完成了，就要判断是否最后一个动作
     * 如果不是最后一个动作的话，就要跳到下一个动作，如果该动作是已经完成了，
     * 就要再跳下一个动作*/
    override fun addCount(): Boolean {
        //判断是否结束
        return if (finish) {
            false
        } else {
            var action = actionContainer[viewModel.currentPosition.value!!]
            currentCount += 1
            action.finishCount += 1
            return if (action.finishCount == action.actionCount) {      // 2 /2 组数+1
                //完成了该动作，需要切换组数
                addGroup()
            } else if (action.finishCount > action.actionCount) {
                action.finishCount = 1
                upDataUI()
                true
            } else {
                if(action.actionCount>3&&action.actionCount-action.finishCount==3&&action.finishGroup==action.setCount-1){
                    LogUtils.i("再坚持3次！加油！3~2~1")
                }
                upDataUI()
                true
            }
        }

    }

    override fun addGroup(): Boolean {
        var action = actionContainer[viewModel.currentPosition.value!!]
        action.finishGroup += 1
        return if (action.finishGroup >= action.setCount) {
            //切换动作
            if (cutAction(true)) {
                if(isFinish){
                    LogUtils.i("最后一个动作！")
                }else{
                    LogUtils.i("下一个动作！Go！")
                }
                action.finishCount = action.setCount
                true
            } else {
                finish = true
                viewModel.finishSport()
                LogUtils.i("恭喜你完成训练！")
                false
            }
        } else {
            //添加组数 并且初始化数量
            upDataUI()
            true
        }
    }

    override fun cutAction(next: Boolean): Boolean {
        return if (finish) {
            false
        } else {
            var value = viewModel.currentPosition.value as Int
            return if (next) {
                if (isFinish) {
                    false
                } else {
                    viewModel.currentPosition.value = value + 1
                    upDataUI()
                    true
                }
            } else {
                if (value > 0) {
                    viewModel.currentPosition.value = value - 1
                    upDataUI()
                    true
                }
                false
            }
        }
    }

    override fun calCount(): Float {
        return 0f
    }

    override fun isFinish(): Boolean {
        return !(viewModel.currentPosition.value!! < actionContainer.size - 1)
    }
}